﻿
using Repositories.Interfaces;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System;
namespace Repositories.Implementations
{
    public class CustomersRepository : BaseRepository<Customers>, ICustomersRepository
    {
        public CustomersRepository()
            : base()
        {
        }
        /// <summary>
        /// Gets the name of the entity set.
        /// </summary>
        /// <value>The name of the entity set.</value>
        protected override string EntitySetName
        {
            get
            {
                return "ASE_DatabaseEntities.Customers";
            }
        }

        public void Refresh()
        {
            ASEDbContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, ASEDbContext.Customers);
        }

        private ICustomersRepository _CustomersRepository;

        /// <summary>
        /// Gets the Customers repository.
        /// </summary>
        /// <value>The Customers repository.</value>
        public ICustomersRepository CustomersRep
        {
            get
            {
                if (_CustomersRepository == null)
                {
                    _CustomersRepository = DIHelper.Resolve<ICustomersRepository>();
                }
                return _CustomersRepository;
            }
        }




        /// <summary>
        /// Creates the Customers.
        /// </summary>
        /// <param name="item">The item.</param>
        public void CreateCustomers(Customers item)
        {
            CustomersRep.Insert(item);
        }

        /// <summary>
        /// Queries the Customers.
        /// </summary>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Query results.</returns>
        public IQueryable<Customers> QueryCustomers(Expression<Func<Customers, bool>> predicate)
        {
            return CustomersRep.GetQuery(predicate);
        }

        /// <summary>
        /// Finds the by ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>The Customers with input ID.</returns>
        public Customers FindById(int id)
        {
            IQueryable<Customers> returnValue = CustomersRep.GetQuery(itm => itm.CustomerId == id);
            if (!returnValue.Any())
                return null;
            return returnValue.FirstOrDefault();
        }

        /// <summary>
        /// Gets all Customers.
        /// </summary>
        /// <returns>all Customers.</returns>
        public List<Customers> GetAllCustomers()
        {
            IQueryable<Customers> returnValue = CustomersRep.GetQuery();
            if (!returnValue.Any())
                return null;
            return returnValue.ToList();
        }

        public List<Customers> GetAllCustomers(string filter)
        {
            if (string.IsNullOrEmpty(filter))
                return GetAllCustomers();
            IQueryable<Customers> returnValue = CustomersRep.GetQuery().Where(c => (c.FirstName + " " + c.LastName).Contains(filter) ||
               c.Mobi.Contains(filter) || c.Road.Contains(filter));

            if (!returnValue.Any())
                return null;
            return returnValue.ToList();
        }

        /// <summary>
        /// Edits the Customers.
        /// </summary>
        /// <param name="item">The item.</param>
        public void EditCustomers(Customers item)
        {
            CustomersRep.Update(item);
        }


        /// <summary>
        /// Deletes the Customers.
        /// </summary>
        /// <param name="item">The item.</param>
        public void DeleteCustomers(Customers item)
        {
            CustomersRep.Delete(item);
        }


        /// <summary>
        /// Deattaches the specified category.
        /// </summary>
        /// <param name="category">The category.</param>
        public void Deattach(Category category)
        {

        }

        /// <summary>
        /// Deattaches the specified Customers.
        /// </summary>
        /// <param name="solicitation">The Customers.</param>
        public void DeattachTo(Customers Customers)
        {
            CustomersRep.DeattachTo(Customers);
        }

        /// <summary>
        /// Applies the changes to database.
        /// </summary>
        public void ApplyTheChanges()
        {
            CustomersRep.UnitOfWork.SaveChanges();
        }

        /// <summary>
        /// Accepts all changes.
        /// </summary>
        public void AcceptAllChanges()
        {
            CustomersRep.UnitOfWork.AcceptAllChanges();
        }


    }
}
